home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / Booting Gallery / Booting Gallery (source) / Sources / Blasteroids / BlasteroidsShipSprite.cpp copy < prev    next >
Encoding:
Text File  |  1996-06-22  |  2.7 KB  |  153 lines  |  [TEXT/CWIE]

  1. /***
  2.  *     Created by Bill Hubauer on Fri, Jun 21, 1996 @ 2:53 AM.
  3.  *
  4.  ***/
  5.  
  6.  
  7. #ifndef __BlasteroidsShipSprite_H__
  8. #include "BlasteroidsShipSprite.h"
  9. #endif
  10.  
  11.  
  12. void    CBlasteroidsShipSprite::UpdatePosition() //Override
  13. {
  14.     if(SpaceDownQ()){
  15.         FireGun();
  16.     }else{
  17.         
  18.         short            newHPos = GetLocation()->left;
  19.         short            newVPos = GetLocation()->top;
  20.         const Rect&        bounds = *GetGameBounds();
  21.         
  22.         if(LeftArrowQ()){
  23.             newHPos -= kMoveHInterval;
  24.         }else if(RightArrowQ()){
  25.             newHPos += kMoveHInterval;
  26.         }
  27.         
  28.         if(newHPos < bounds.left){
  29.             newHPos = bounds.left;
  30.         }else if(newHPos > (bounds.right - Width())){
  31.             newHPos = bounds.right - Width();
  32.         }
  33.         
  34.         if(UpArrowQ()){
  35.             newVPos -= kMoveVInterval;
  36.         }else if(DownArrowQ()){
  37.             newVPos += kMoveVInterval;
  38.         }
  39.         
  40.         if(newVPos < bounds.top){
  41.             newVPos = bounds.top;
  42.         }else if(newVPos > (bounds.bottom - Height())){
  43.             newVPos = bounds.bottom - Height();
  44.         }
  45.         
  46.         MoveBy(newHPos - GetLocation()->left, newVPos - GetLocation()->top);
  47.     }
  48. }
  49.  
  50.  
  51. static short CalcShipTop(CSpriteWorld* world)
  52. {
  53.     const Rect&        bounds = *(world->GetSpriteCanvas()->GetBounds());
  54.     
  55.     return (bounds.bottom + bounds.top - 32) / 2;
  56. }
  57.  
  58.  
  59. inline short    RectWidth(const Rect& r)
  60. {
  61.     return r.right - r.left;
  62. }
  63.  
  64.  
  65. static short CalcShipLeft(CSpriteWorld* world)
  66. {
  67.     const Rect&        bounds = *(world->GetSpriteCanvas()->GetBounds());
  68.  
  69.     return (bounds.left + bounds.right - 32) / 2;
  70. }
  71.  
  72.  
  73. CBlasteroidsShipSprite::CBlasteroidsShipSprite(CSpriteWorld* world,CSpriteGame* game)
  74.     :    CGameSprite(world, game, 0,game->GetImage(kBaseImageID), CalcShipTop(world),
  75.              CalcShipLeft(world), game->GetImageMask(kBaseImageID))
  76. {
  77. }
  78.  
  79.  
  80. CBlasteroidsShipSprite::~CBlasteroidsShipSprite()//Override
  81. {
  82. }
  83.  
  84.  
  85. Boolean    CBlasteroidsShipSprite::LeftArrowQ()
  86. {
  87.     return KeyIsDownQ(0x7B);
  88. }
  89.  
  90.  
  91. Boolean    CBlasteroidsShipSprite::RightArrowQ()
  92. {
  93.     return KeyIsDownQ(0x7C);
  94. }
  95.  
  96.  
  97. Boolean    CBlasteroidsShipSprite::UpArrowQ()
  98. {
  99.     return KeyIsDownQ(0x7E);         
  100. }
  101.  
  102.  
  103. Boolean    CBlasteroidsShipSprite::DownArrowQ()
  104. {
  105.     return KeyIsDownQ(0x7D);
  106. }
  107.  
  108.  
  109. Boolean    CBlasteroidsShipSprite::SpaceDownQ()
  110. {
  111.     return KeyIsDownQ(0x31);
  112. }
  113.  
  114.  
  115. void    CBlasteroidsShipSprite::FireGun()
  116. {
  117.     short        deltaV,deltaH;
  118.     
  119.     deltaV = -75;
  120.     deltaH = 0;
  121.     
  122.     new CBlasteroidsBulletSprite(GetWorld(), GetGame(), GetLocation()->top, GetLocation()->left, 
  123.         deltaV, deltaH);
  124.     
  125. }
  126.  
  127.  
  128. CBlasteroidsBulletSprite::CBlasteroidsBulletSprite(CSpriteWorld* world,CSpriteGame* game,short startTop,short startLeft,
  129.         short deltaV,short deltaH)
  130.     :    CGameSprite(world, game, 0, game->GetImage(kBulletImageID), startTop, startLeft, 
  131.             game->GetImageMask(kBulletImageID)),
  132.         fDeltaV(deltaV),
  133.         fDeltaH(deltaH)
  134. {
  135.     
  136. }
  137.  
  138.  
  139. CBlasteroidsBulletSprite::~CBlasteroidsBulletSprite()//Override
  140.  
  141. {
  142.  
  143. }
  144.  
  145.  
  146. void    CBlasteroidsBulletSprite::UpdatePosition()//Override
  147. {
  148.     MoveBy(fDeltaH,fDeltaV);
  149.     if(OutOfBoundsQ()){
  150.         delete this;
  151.     }
  152. }
  153.